﻿using CSRedis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace xycn.common
{
    /// <summary>
    /// RedisHelper
    /// </summary>
    public class RedisHelper
    {
        private static string config = XmlConfigHelper.GetConfigToString("/Config/RedisConnString");

        /// <summary>
        /// 读取Redis
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="dbConnName">自定义redis连接串 为null时去配置文件默认值</param>
        /// <returns></returns>
        public static T GetRedis<T>(string key, string dbConnName = null)
        {
            CSRedisClient clientq = new CSRedisClient(dbConnName ?? config);
            using (var client = clientq)
            {
                return clientq.Get<T>(key);
            }
        }
        /// <summary>
        /// 读取Redis
        /// </summary>
        /// <param name="key"></param>
        /// <param name="dbConnName">自定义redis连接串 为null时去配置文件默认值</param>
        /// <returns></returns>
        public static string GetRedis(string key, string dbConnName = null)
        {
            CSRedisClient clientq = new CSRedisClient(dbConnName ?? config);
            using (var client = clientq)
            {
                return clientq.Get(key);
            }
        }


        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="key"></param>
        /// <param name="dbConnName">自定义redis连接串 为null时去配置文件默认值</param>
        public static void DelRedis(string key, string dbConnName = null)
        {
            CSRedisClient clientq = new CSRedisClient(dbConnName ?? config);
            using (var client = clientq)
            {
                clientq.Del(key);
            }
        }

        /// <summary>
        /// 设置redis
        /// </summary>
        /// <param name="key">key</param>
        /// <param name="val">值</param>
        /// <param name="dbConnName">自定义redis连接串 为null时去配置文件默认值</param>
        /// <returns>结果</returns>
        public static bool SetRedis(string key, string val, string dbConnName = null)
        {
            CSRedisClient clientq = new CSRedisClient(dbConnName ?? config);
            using (var client = clientq)
            {
                return clientq.Set(key, val);
            }
        }

        /// <summary>
        /// 设置redis
        /// </summary>
        /// <param name="key">key</param>
        /// <param name="val">值</param>
        /// <param name="dbConnName">自定义redis连接串 为null时去配置文件默认值</param>
        /// <returns>结果</returns>
        public static bool SetRedisObj(string key, object val, string dbConnName = null)
        {
            CSRedisClient clientq = new CSRedisClient(dbConnName ?? config);
            using (var client = clientq)
            {
                return clientq.Set(key, val);
            }
        }

        /// <summary>
        /// 设置redis
        /// </summary>
        /// <param name="key">key</param>
        /// <param name="val">值</param>
        /// <param name="seconds">过期时间 单位秒</param>
        /// <param name="dbConnName">自定义redis连接串 为null时去配置文件默认值</param>
        /// <returns>结果</returns>
        public static bool SetRedis(string key, string val, int seconds, string dbConnName = null)
        {
            CSRedisClient clientq = new CSRedisClient(dbConnName ?? config);
            using (var client = clientq)
            {
                return clientq.Set(key, val, seconds);
            }
        }

        /// <summary>
        /// 设置redis
        /// </summary>
        /// <param name="key">key</param>
        /// <param name="val">值</param>
        /// <param name="seconds">过期时间 单位秒</param>
        /// <param name="dbConnName">自定义redis连接串 为null时去配置文件默认值</param>
        /// <returns>结果</returns>
        public static bool SetRedisObj(string key, object val, int seconds, string dbConnName = null)
        {
            CSRedisClient clientq = new CSRedisClient(dbConnName ?? config);
            using (var client = clientq)
            {
                return clientq.Set(key, val, seconds);
            }
        }
    }
}
